﻿Imports System.Activities
Imports System.Management
Imports System.Activities.Presentation.Metadata
Imports System.Drawing
Imports System.ComponentModel
Imports System.Activities.Presentation

Public Class WMISYSSetTerminalService
    Inherits WMIExecuteActivityBase

    Public Property UseRemoteDesktop As InArgument(Of Boolean)
    Public Property SetFirewallRule As InArgument(Of Boolean)
    Public Property IsVistaLater As InArgument(Of Boolean)

    Private _useRDP As Boolean = False
    Private _setFW As Boolean = False
    Private _laterVista As Boolean = True


    Public Sub New()
        Me.DisplayName = My.Resources.SetTerminalServiceDisplayName
    End Sub

    Protected Overrides Function BeginExecute(ByVal context As AsyncCodeActivityContext, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        MyBase._target = "Win32_TerminalServiceSetting"
        MyBase._where = ""
        MyBase._service = "cimv2"
        _useRDP = context.GetValue(Me.UseRemoteDesktop)
        _setFW = context.GetValue(Me.SetFirewallRule)
        _laterVista = context.GetValue(Me.IsVistaLater)

        Return MyBase.BeginExecute(context, callback, state)
    End Function

    Protected Overrides Function ExecuteWMI(ByVal target As String, ByVal whereCondition As String, ByVal wmScope As ManagementScope) As List(Of Hashtable)
        Dim result As New List(Of Hashtable)
        Try
            wmScope = CreateScope(wmScope)

            Dim wmQuery As New ObjectQuery("SELECT * FROM " + target)
            Dim wmResult As New ManagementObjectSearcher(wmScope, wmQuery)
            For Each res As ManagementObject In wmResult.Get
                Dim inParams = res.GetMethodParameters("SetAllowTSConnections")
                If _useRDP Then
                    inParams("AllowTSConnections") = 1
                Else
                    inParams("AllowTSConnections") = 0
                End If
                If _setFW Then
                    inParams("ModifyFirewallException") = 1
                Else
                    inParams("ModifyFirewallException") = 0
                End If

                Dim outParams = res.InvokeMethod("SetAllowTSConnections", inParams, Nothing)
                If outParams IsNot Nothing Then
                    Dim resRec As New Hashtable
                    For Each resCol In outParams.Properties
                        If resCol.Value Is Nothing Then
                            resRec.Add(resCol.Name, Nothing)
                        Else
                            resRec.Add(resCol.Name, resCol.Value.ToString)
                        End If
                    Next
                    result.Add(resRec)
                End If
            Next
        Catch ex As Exception
            result.Clear()
        End Try
        Return result
    End Function

    Protected Overrides Function CreateScope(wmScope As ManagementScope) As ManagementScope
        If wmScope Is Nothing Then
            If _laterVista Then
                wmScope = New ManagementScope("\\" + My.Computer.Name + "\root\" + _service + "\TerminalServices")
            Else
                wmScope = New ManagementScope("\\" + My.Computer.Name + "\root\" + _service)
            End If
        End If
        If Not wmScope.IsConnected Then wmScope.Connect()
        Return wmScope
    End Function

    Protected Overrides Sub CacheMetadata(metadata As CodeActivityMetadata)
        MyBase.CacheMetadata(metadata)

        Dim build As New AttributeTableBuilder
        'WMISYSSetServiceStatus Activity
        build.AddCustomAttributes(Me.GetType,
                                      New DesignerAttribute(GetType(WMIDefaultActivityDesigner), GetType(ActivityDesigner)),
                                      New ToolboxBitmapAttribute(Me.GetType, "WmiDefault.png"))

        build.AddCustomAttributes(GetType(WMISYSSetTerminalService), "UseRemoteDesktop",
                              New CategoryAttribute(My.Resources.CategoryDetail),
                              New DisplayNameAttribute(My.Resources.UseRemoteDesktopDisplay),
                              New DescriptionAttribute(My.Resources.UseRemoteDesktopDescription))
        build.AddCustomAttributes(GetType(WMISYSSetTerminalService), "SetFirewallRule",
                              New CategoryAttribute(My.Resources.CategoryDetail),
                              New DisplayNameAttribute(My.Resources.SetFirewallRuleDisplay),
                              New DescriptionAttribute(My.Resources.SetFirewallRuleDescription))
        build.AddCustomAttributes(GetType(WMISYSSetTerminalService), "IsVistaLater",
                              New CategoryAttribute(My.Resources.CategoryDetail),
                              New DisplayNameAttribute(My.Resources.IsVistaLaterDisplay),
                              New DescriptionAttribute(My.Resources.IsVistaLaterDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub

End Class
